package com.macoli.mock_server.httpserver.route

import com.macoli.mock_server.httpserver.beans.Destnation
import com.macoli.mock_server.httpserver.beans.HTTP_METHOD
import com.macoli.mock_server.httpserver.beans.RouteItem
import javax.inject.Inject

class Router @Inject constructor() {
    private val routingTable : HashMap<String , RouteItem> = HashMap<String , RouteItem>()

    fun route(uri : String , params : Map<String, List<String>>) : Destnation? {
        val routeItem = routingTable[uri]
        if (routeItem != null) {
            return routeItem.matchCondition(params)
        }
        return null
    }

    fun addRouteItem(method: HTTP_METHOD , uri : String , defaultPath : String , static : Boolean) {
        routingTable[uri] = RouteItem(method , uri , defaultPath , static)
    }

    fun addDestnation(destnation: Destnation) {
        if (routingTable.containsKey(destnation.uri)) {
            routingTable[destnation.uri]?.addDestnation(false , destnation.destnation ,
                destnation.static , destnation.predicate)
        }
    }
}